<?php
namespace app\admin\controller;
use think\Db;

class Statistics extends Common
{
	public function index(){

		// $data = Db::name('topic')->where('status',1)->select();
		// // dump($data);die;
		//平均分
		$score = 0;

		//及格率
		$pass_rate = 0;
		//优秀率
		$excellence_rate = 0;

		//挂科人数
		$dis_pass = 0;

		$num = 0;

		$data = model("Statistics")->getDataAll(1);

		$major = Db::name('major')->where('status',1)->select();
		$this->assign('major',$major);

		$subject = Db::name('subject')->where('status',1)->select();
		$this->assign('subject',$subject);

		// dump($data);die;
		foreach ($data as $key => $value) {
			//平均分
			$score += (int)$value['scroe'];

			//及格率
			if((int)$value['scroe'] >= 60){
				$pass_rate += 1;
			}

			//优秀率
			if((int)$value['scroe'] >= 80){
				$excellence_rate += 1;
			}

			//挂科人数
			if((int)$value['scroe'] < 60){
				$dis_pass += 1;
			}

		}
		//平均分
		if(count($data) == 0){
			$num = 1;
		}else{
			$num = count($data);
		}

		$score = $score/$num;

		$pass_rate = $pass_rate/$num;

		$excellence_rate = $excellence_rate/$num;

		$data2 = ['score'=>$score,'pass_rate'=>$pass_rate,'excellence_rate'=>$excellence_rate,'dis_pass'=>$dis_pass];
		// dump($data2);die;
		$this->assign('data2',$data2);

		// dump($score);die;
		$this->assign('data',$data);
		return $this->fetch();
	}

	 //软删除
    public function del(){
    	//获取的是一个数组
    	$ids = input('post.ids/a');
    	//将数组转为字符串 以逗号分隔
    	$ids = implode(',',$ids);
    	//软删除 更新状态
    	$result = Db::name('single')->where("id in ($ids)")->setField('status',0);
    	if($result){
    		$this->success("删除成功");
    	}else{
    		$this->error("删除失败");
    	}
    }
    //选着二级联动
    public function major(){
    	$major_id = input('post.major_id');
    	$data = Db::name('subject')->where('major_id',$major_id)->select();
    	return json($data);
    } 
	
    
}